#!/usr/bin/env python
# encoding: utf-8

"""
@author: Cao
@contact: caoqiang0012@163.com
@file: tickets.py
@time: 12/12/2016 12:40 PM

命令行火车票查看器

Usage:
    tickets [-gdtkz] <from> <to> <date>

Options:
    -h,--help   显示帮助菜单
    -g          高铁
    -d          动车
    -t          特快
    -k          快速
    -z          直达

Example:
    tickets 北京 上海 2016-10-10
    tickets -dg 成都 南京 2016-10-10
"""
from docopt import docopt
from stations import stations
import requests
from trainscollection import TrainsCollection

def cli():
    """Command-line interface"""
    arguments = docopt(__doc__)
    fromstation = stations.get(arguments['<from>'])
    tostation = stations.get(arguments['<to>'])
    date = arguments['<date>']

    #构建URL
    url = 'https://kyfw.12306.cn/otn/lcxxcx/query?purpose_codes=ADULT&queryDate={}&from_station={}&to_station={}'.format(
        date, fromstation,tostation
    )
    options = ''.join([
        key for key, value in arguments.items() if value is True
    ])
    result = requests.get(url, verify=False)
    available_trains = result.json()['data']['datas']
    TrainsCollection(available_trains, options).pretty_print()

if __name__ == '__main__':
    cli()
